<template>
    <div>
        <div>
            <el-input
                    size="small"
                    class="addPosInput"
                    placeholder="添加职位"
                    prefix-icon="el-icon-plus"
                    v-model="pos.name"
                    @keydown.enter.native="addPositions">
            </el-input>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="addPositions">添加</el-button>
        </div>
        <div class="posManaMain">
            <el-table
                    :data="positions"
                    border
                    stripe
                    size="small"
                    style="width: 60%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                title="修改职位名称"
                :visible.sync="dialogVisible"
                width="30%">
           <div>
               <div>
                   <el-tag>职位名称</el-tag>
                   <el-input class="updatePosInput" size="small" v-model="updatePos.name"></el-input>
               </div>
               <div style="margin-top: 5px;">
                   <el-tag style="margin-right: 10px">是否启用</el-tag>
                   <el-switch
                           v-model="updatePos.enabled"
                           active-text="启用"
                           inactive-text="禁用">
                   </el-switch>
               </div>
           </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doUpdate()">确 定</el-button>
  </span>
        </el-dialog>
        <el-button type="danger" style="margin-top: 10px" :disabled="multipleSelection.length==0" @click="deletePositions()">批量删除</el-button>
    </div>

</template>

<script>
    export default {
        name: "PosMana",
       data(){
           return {
               pos :{
                   name : ''
               },
               dialogVisible:false,
               positions: [],
               updatePos:{
                   name:'',
                   enabled:''
               },
               multipleSelection: []
           }
       },
        mounted(){
            this.initPositions();
        },
        methods:{
            //批量删除
            deletePositions() {
                this.$confirm('此操作将永久删除' + this.multipleSelection.length + '条职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    var ids = this.multipleSelection.map(item => item.id).join()//获取所有选中行的id组成的字符串，以逗号分隔
                    console.log(ids)
                    this.deleteRequest('/api/system/basic/pos/many?ids='+ids).then(resp=>{
                        if(resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //多选框改变时调用方法
            handleSelectionChange(val){
                console.log(val);
                this.multipleSelection = val;
            },
            //打开对话框
            showEditView(index,data){
                this.dialogVisible = true;
               // this.updatePos=data;
                Object.assign(this.updatePos,data);
            },
            //修改
            doUpdate(){
                console.log(this.updatePos);
                if(this.updatePos.name){
                    this.putRequest('/api/system/basic/pos/',this.updatePos).then(resp=>{
                        if(resp){
                            this.initPositions();
                            this.dialogVisible = false;
                        }
                    })
                }else{
                    this.$message.error('职位名称不能为空');
                }
            },
            //添加
            addPositions(){
                if(this.pos.name){
                    this.postRequest('/api/system/basic/pos/',this.pos).then(resp=>{
                        if(resp){
                            console.log(resp);
                            this.initPositions();
                            this.pos.name='';
                        }
                    })
                }else {
                    this.$message.error('职位名称不能为空');
                }
            },
            //初始化数据
            initPositions(){
                this.getRequest('/api/system/basic/pos/').then(resp=>{
                    if(resp){
                        this.positions = resp;
                    }
                })
            },
            //删除
            handleDelete(index,data){
                console.log(data);
                this.$confirm('此操作将永久删除'+data.name+'职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    this.deleteRequest('/api/system/basic/pos/' + data.id).then(resp=>{
                        if(resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    }
</script>

<style scoped>

    .addPosInput {
        width: 300px;
        margin-right: 8px
    }
    .posManaMain {
        margin-top: 10px;
    }

    .updatePosInput {
        width: 200px;
        margin-left: 8px;
    }
</style>